package org.zjx.analysis.analyze.game;

import lombok.Getter;
import org.zjx.analysis.analyze.AbstractImageAnalyzer;
import org.zjx.analysis.analyze.battle.BattleAnalysis;
import org.zjx.analysis.identify.IdentifyFontUtil;
import org.zjx.analysis.util.PaneUtil;
import org.zjx.command.entity.screen.Address;
import org.zjx.command.enums.FontType;
import org.zjx.command.util.ImageUtil;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Getter
public class GameAnalysis extends AbstractImageAnalyzer {
    public static BufferedImage inTeamImage = ImageUtil.createImage("iVBORw0KGgoAAAANSUhEUgAAAC4AAAABCAYAAAChQ5zUAAAAD0lEQVR42mO4cWLF/6GIAXVZlsNKr8GqAAAAAElFTkSuQmCC");

    private final BufferedImage screenshot;

    private int hpPercentageRole; //血量百分比
    private int injuryPercentageRole; //伤势百分比
    private int mpPercentageRole; //蓝量百分比
    private int exePercentageRole; //经验百分比
    private int ragePercentageRole; //愤怒百分比

    private int hpPercentagePet; //血量百分比
    private int mpPercentagePet; //蓝量百分比
    private int exePercentagePet; //经验百分比

    private boolean payment; //欠费

    private boolean haveWindow; //有打开的窗口

    private Address address;
    private boolean intBattle; //战斗中
    private boolean inTeam;    //有队中

    private AttributeAnalysis attributeAnalysis;
    private BattleAnalysis battleAnalysis;

    public GameAnalysis(IWindow window) {
        super("主界面", "主界面");
        screenshot = window.getScreenshot();
        analyze(screenshot);
    }

    @Override
    protected void initResources() {

    }

    @Override
    protected boolean isSceneMatch(BufferedImage image) {
        return false;
    }

    private Address analysisAddress() {
        BufferedImage addressImage = ImageUtil.cutImage(screenshot, addressArea());
        PaneUtil paneUtil = new PaneUtil(addressImage, 3, Collections.singletonList(new Color(104, 100, 104)));
        List<Rectangle> rects = paneUtil.detectRect();
        if (rects.isEmpty()) return null;
        else {
            Rectangle rectangle = conversion(addressArea(),rects.get(0));
            BufferedImage image = ImageUtil.cutImage(screenshot, rectangle);
            String addressStr = IdentifyFontUtil.identifyAsString(image,Collections.singletonList(Color.WHITE), FontType.LOCATION, 0, false);
            return matcherLocation(addressStr);
        }
    }

    public Rectangle conversion(Rectangle windowArea, Rectangle rect) {
        return new Rectangle(rect.x + windowArea.x, rect.y + windowArea.y, rect.width, rect.height);
    }

    public boolean analysisInTeam() {
        BufferedImage image = ImageUtil.cutImage(screenshot, new Rectangle(520, 0, inTeamImage.getWidth(), inTeamImage.getHeight()));
        int count = 0;
        for (int j = 0; j < image.getWidth(); j++) {
            if (image.getRGB(j, 0) == inTeamImage.getRGB(j, 0)) count++;
        }
        return count > inTeamImage.getWidth() / 2;
    }


    public Rectangle addressArea() {
        return ImageUtil.createRect(11, 23, 137, 54);
    }

    private Address matcherLocation(String location) {
        String regex = "([\\w\\u4e00-\\u9fa5]+)【([0-9]+)，([0-9]+)】";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(location);
        if (matcher.find()) {
            String city = matcher.group(1);
            int x = Integer.parseInt(matcher.group(2));
            int y = Integer.parseInt(matcher.group(3));
            return new Address(city, x, y);
        } else {
            return null;
        }
    }

    @Override
    public void analyze(BufferedImage image) {

        this.address = analysisAddress();

        attributeAnalysis = new AttributeAnalysis(screenshot);
        battleAnalysis = new BattleAnalysis(screenshot);

        intBattle = battleAnalysis.isBattle();
        hpPercentageRole = attributeAnalysis.getHpPercentageRole();
        injuryPercentageRole = attributeAnalysis.getInjuryPercentageRole();
        mpPercentageRole = attributeAnalysis.getMpPercentageRole();
        ragePercentageRole = attributeAnalysis.getRagePercentageRole();
        exePercentageRole = attributeAnalysis.getExePercentageRole();
        hpPercentagePet = attributeAnalysis.getHpPercentagePet();
        mpPercentagePet = attributeAnalysis.getMpPercentagePet();
        exePercentagePet = attributeAnalysis.getExePercentagePet();

        inTeam = analysisInTeam();

    }
}
